package com.siwei.mes.service.experiment.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.siwei.mes.common.query.PageQuery;
import com.siwei.mes.common.util.ScriptUtils;
import com.siwei.mes.entity.experiment.CheckBatchConfig;
import com.siwei.mes.entity.experiment.CheckQuickConfig;
import com.siwei.mes.entity.vo.CheckConfigVo;
import com.siwei.mes.enums.CheckEnum;
import com.siwei.mes.mapper.experiment.CheckBatchConfigMapper;
import com.siwei.mes.mapper.experiment.CheckQuickConfigMapper;
import com.siwei.mes.mapper.experiment.ExperimentCheckItemorderMapper;
import com.siwei.mes.mapper.experiment.ExperimentCheckTypeRecordMapper;
import com.siwei.mes.service.experiment.CheckBatchConfigService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 批检指标配置(CheckBatchConfig)表服务实现类
 *
 * @author linzi
 * @since 2023-11-25 22:22:18
 */
@Slf4j
@Service
public class CheckBatchConfigServiceImpl implements CheckBatchConfigService {
    @Resource
    private CheckBatchConfigMapper checkBatchConfigMapper;
    @Resource
    private ExperimentCheckItemorderMapper experimentCheckItemorderMapper;
    @Resource
    private CheckQuickConfigMapper checkQuickConfigMapper;
    @Resource
    private ExperimentCheckTypeRecordMapper experimentCheckTypeRecordMapper;

    /**
     * 组装指标用于后面通过js去执行
     *
     * @param entity
     */
    private void payloadFormula(CheckBatchConfig entity) {
        /**组装合格指标 用于后面通过js去执行*/
        String qualifiedFormula = ScriptUtils.makeFormula(entity.getQualifiedCheckOneType(), entity.getQualifiedCheckOneValue(), entity.getQualifiedCheckTwoType(), entity.getQualifiedCheckTwoValue());
        entity.setQualifiedCheckText(qualifiedFormula);
        /**组装让步指标  用于后面通过js去执行*/
        String unQualifiedFormula = ScriptUtils.makeFormula(entity.getUnQualifiedCheckOneType(), entity.getUnQualifiedCheckOneValue(), entity.getUnQualifiedCheckTwoType(), entity.getUnQualifiedCheckTwoValue());
        entity.setUnQualifiedCheckText(unQualifiedFormula);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long create(CheckBatchConfig entity) {
        List<CheckBatchConfig> list = checkBatchConfigMapper.getList(new CheckBatchConfig().setTestProjectCode(entity.getTestProjectCode()));
        assertTrue(CollectionUtils.isNotEmpty(list), "试验项目已存在");
        entity.init(true);
        /**组装指标 用于后面通过js去执行*/
        this.payloadFormula(entity);
        checkBatchConfigMapper.insertSelective(entity);

        /**只要操作了就清空   2025-03-21 不需要清空了*/
        //log.info("修改批检指标，开始清空【experimentCheckItmeorder】表所有批检关联关系，对应projectCategory：【{}】", entity.getProjectCategory());
        //if (entity.getProjectCategory().equals(MaterialsEnum.CONCRETE.getCode())) {
        //    experimentCheckItemorderMapper.deleteByCheckType(CheckEnum.BATCH.getCode());
        //}
        return entity.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(CheckBatchConfig entity) {
        CheckBatchConfig checkBatchConfig = checkBatchConfigMapper.selectByPrimaryKey(entity.getId());
        assertTrue(checkBatchConfig == null, "批检指标配置信息不存在");
        if (!entity.getTestProjectCode().equals(checkBatchConfig.getTestProjectCode())) {
            List<CheckBatchConfig> list = checkBatchConfigMapper.getList(new CheckBatchConfig().setTestProjectCode(entity.getTestProjectCode()));
            assertTrue(CollectionUtils.isNotEmpty(list), "试验项目已存在");
        }
        /**组装指标 用于后面通过js去执行*/
        this.payloadFormula(entity);
        entity.init(false);
        checkBatchConfigMapper.updateByPrimaryKeySelective(entity);

        /**修改允许为null 指标2 相关数据*/
        checkBatchConfigMapper.updateIsNullInfo(entity);

        /**只要操作了就清空  2025-03-21 不需要清空了*/
        //log.info("修改批检指标，开始清空【experimentCheckItmeorder】表所有批检关联关系，对应projectCategory：【{}】", entity.getProjectCategory());
        //if (entity.getProjectCategory().equals(MaterialsEnum.CONCRETE.getCode())) {
        //    experimentCheckItemorderMapper.deleteByCheckType(CheckEnum.BATCH.getCode());
        //}
        //
        //experimentCheckTypeRecordMapper.deleteByCheckType(CheckEnum.BATCH.getCode(), entity.getProjectCategory());

    }

    @Override
    public void delete(Long id) {
        CheckBatchConfig checkBatchConfig = checkBatchConfigMapper.selectByPrimaryKey(id);
        assertTrue(checkBatchConfig == null, "批检指标配置信息不存在");
        checkBatchConfigMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<CheckBatchConfig> getPage(PageQuery<CheckBatchConfig, CheckBatchConfig> query) {
        CheckBatchConfig searchParam = query.getParams();
        return PageHelper.startPage(query.getPageNum(), query.getPageSize())
                .doSelectPageInfo(() -> checkBatchConfigMapper.getList(searchParam));
    }

    @Override
    public CheckBatchConfig loadById(Long id) {
        return checkBatchConfigMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<CheckBatchConfig> getAll(CheckBatchConfig entity) {
        return checkBatchConfigMapper.getList(entity);
    }

    @Override
    public void updateByPrimaryKeySelective(CheckBatchConfig entity) {
        CheckBatchConfig checkBatchConfig = checkBatchConfigMapper.selectByPrimaryKey(entity.getId());
        assertTrue(checkBatchConfig == null, "批检指标配置信息不存在");
        checkBatchConfigMapper.updateByPrimaryKeySelective(entity.init(false));
    }

    @Override
    public Map<String, String> getCheckConfig(CheckConfigVo vo) {
        Map<String, String> configMap = new LinkedHashMap<>();
        CheckQuickConfig checkQuickConfig = new CheckQuickConfig().setProjectCategory(vo.getProjectCategory());
        List<CheckQuickConfig> checkQuickConfigList = checkQuickConfigMapper.getList(checkQuickConfig);
        CheckBatchConfig checkBatchConfig = new CheckBatchConfig().setProjectCategory(vo.getProjectCategory());
        List<CheckBatchConfig> checkBatchConfigList = checkBatchConfigMapper.getList(checkBatchConfig);
        if (CheckEnum.QUICK.getCode().equals(vo.getCheckType())) {
            for (CheckQuickConfig quickConfig : checkQuickConfigList) {
                configMap.put(quickConfig.getTestProjectCode(), quickConfig.getTestProjectName());
            }
        } else if (CheckEnum.BATCH.getCode().equals(vo.getCheckType())) {
            for (CheckBatchConfig batchConfig : checkBatchConfigList) {
                configMap.put(batchConfig.getTestProjectCode(), batchConfig.getTestProjectName());
            }
        } else {
            for (CheckQuickConfig quickConfig : checkQuickConfigList) {
                configMap.put(quickConfig.getTestProjectCode(), quickConfig.getTestProjectName());
            }
            for (CheckBatchConfig batchConfig : checkBatchConfigList) {
                configMap.put(batchConfig.getTestProjectCode(), batchConfig.getTestProjectName());
            }
        }
        return configMap;
    }
}
